import React from 'react'
import PickerOverView from './core/PickerOverView'

// 如有需要可以制定-theme
import {RXPickerStyle} from 'react-native-rxpicker'
const rxpCon = {
  btnLeftBar: {text: '取消'},
  titleText: {text: '请选择'},
  btnRightBar: {text: '确定'},
}
RXPickerStyle.initApi(rxpCon)

import SinglePicker from './components/SinglePicker'
import {RXDate} from 'react-native-rxpicker'
import DhSingle from './data/dhSingle'

const _pk = React.createRef(null)

// 一、入口配置页面
const PickerOver = () => {
  return <PickerOverView ref={_pk} />
}
export {PickerOver}

// 二、具体页面中 使用方法
/**
 * 页面中直接调用方法
 * @showSaveDays 选择日志保留日期
 * @showGuanXian 选择当前官衔
 */
export default {
  /**
   * 选择日志保留日期
   * @param {object} props {overClickEnable, ...other}
   */
  showSaveDays: props => {
    const {enable, other} = expProps(props)
    if (!enable) return
    _pk.current.show(
      <SinglePicker
        {...other}
        title={'选择天数'}
        list={DhSingle.selDays}
        _superCallBack={_pk.current.dismiss}
      />,
    )
  },
  /**
   * 选择当前官衔
   * @param {object} props {overClickEnable, ...other}
   */
  showGuanXian: props => {
    const {enable, other} = expProps(props)
    if (!enable) return
    _pk.current.show(
      <SinglePicker
        {...other}
        title={'选择官衔'}
        list={DhSingle.selGuanXian}
        _superCallBack={_pk.current.dismiss}
      />,
    )
  },
  showSingle: props => {
    const {enable, other} = expProps(props)
    console.log('showSingle enable=', enable)
    console.log('other', other)
    if (!enable) return
    _pk.current.show(<SinglePicker {...other} _superCallBack={_pk.current.dismiss} />)
  },
  // 测试
  showNumCount: props => {
    const {enable, other} = expProps(props)
    if (!enable) return
    _pk.current.show(
      <SinglePicker
        {...other}
        title={'时间选择1'}
        list={RXDate.RXADay24Hours(true)}
        _superCallBack={_pk.current.dismiss}
      />,
    )
  },
  dismiss: () => {
    if (!isEnable()) {
      console.log('.. RxPicker.dismiss 初始化失败了？？？ UtilPic=nil')
      return
    }
    _pk.current.dismiss()
  },
}

// ---------------
const isEnable = () => {
  if (_pk && typeof _pk === 'object') {
    return true
  }
  return false
}

function expProps(props) {
  function Coce(e) {
    let enable = true
    if (typeof e === 'boolean') {
      console.log('overClickEnable=', e)
      enable = e
    }
    console.log('enable=', enable, '--e=', e)
    _pk.current.changOverClickEnable(enable)
  }
  const {overClickEnable, ...other} = props
  console.log('pk show  _pk=', _pk)
  if (!isEnable()) {
    console.log('.. RxPicker.showNumCount 初始化失败了？？？ UtilPic=nil')
    return false, null
  }
  Coce(overClickEnable)
  return {enable: true, other}
}
